package com.test.dababy.dababy_07_loan_status;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Feed_dababybackActivity;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_00_utils.DababyConfigs;
import com.test.dababy.dababy_00_analytics.DababyUtil.DababyApRvUtil;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

public class Rate_dababyActive_dababyDialog extends Dialog {

    private String sct1="";
    private int lhLb=38;
    private String drCL="";
    private boolean g4fo=false;
    private int j11j=33;
    private char fXoR='u';
    private String cGLp="";
    private String p8HK="";
    private float wDJS=0.928379f;
    private String y9S4="";
    private float ht45=0.55760926f;
    private float a3ir=0.006599009f;

    public void g5UB5(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getSct1())) setSct1(getDrCL()+getLhLb()+isG4fo()+getJ11j()+getfXoR()+getcGLp());
                else setP8HK(getY9S4()+getwDJS()+getHt45()+getA3ir());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setSct1(String sct1) {
        this.sct1 = sct1;
    }

    public void setLhLb(int lhLb) {
        this.lhLb = lhLb;
    }

    public void setDrCL(String drCL) {
        this.drCL = drCL;
    }

    public void setG4fo(boolean g4fo) {
        this.g4fo = g4fo;
    }

    public void setJ11j(int j11j) {
        this.j11j = j11j;
    }

    public void setfXoR(char fXoR) {
        this.fXoR = fXoR;
    }

    public void setcGLp(String cGLp) {
        this.cGLp = cGLp;
    }

    public void setP8HK(String p8HK) {
        this.p8HK = p8HK;
    }

    public void setwDJS(float wDJS) {
        this.wDJS = wDJS;
    }

    public void setY9S4(String y9S4) {
        this.y9S4 = y9S4;
    }

    public void setHt45(float ht45) {
        this.ht45 = ht45;
    }

    public void setA3ir(float a3ir) {
        this.a3ir = a3ir;
    }

    public String getSct1() {
        return sct1;
    }

    public int getLhLb() {
        return lhLb;
    }

    public String getDrCL() {
        return drCL;
    }

    public boolean isG4fo() {
        return g4fo;
    }

    public int getJ11j() {
        return j11j;
    }

    public char getfXoR() {
        return fXoR;
    }

    public String getcGLp() {
        return cGLp;
    }

    public String getP8HK() {
        return p8HK;
    }

    public float getwDJS() {
        return wDJS;
    }

    public String getY9S4() {
        return y9S4;
    }

    public float getHt45() {
        return ht45;
    }

    public float getA3ir() {
        return a3ir;
    }

    private final String url_dababy = "https://play.google.com/store/apps/details?id=" + DababyConfigs.PACK_dababyAGE_NAME;
    private int score_dababy = 0;
    private static boolean isShow_dababy = false;
    private final ImageButton[] starIbs_dababy = new ImageButton[5];

    private final AppCompatActivity activity_dababy;

    public Rate_dababyActive_dababyDialog(AppCompatActivity activity){
        super(activity);
        this.activity_dababy = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dababy_rate_active);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);


        setCancelable(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);

        findViewById(R.id.ddra_tv_notnow).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_ACTIVITE","CANCEL_CLICK",String.valueOf(score_dababy));
            dismiss();
        });

        TextView textTv = findViewById(R.id.ddra_tv_desc);

        String content = RateAc_dababytiveWind_dababyowConfig.getInstance().getContent_dababy();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs_dababy[0] = findViewById(R.id.ddra_ib_star_1);
        starIbs_dababy[1] = findViewById(R.id.ddra_ib_star_2);
        starIbs_dababy[2] = findViewById(R.id.ddra_ib_star_3);
        starIbs_dababy[3] = findViewById(R.id.ddra_ib_star_4);
        starIbs_dababy[4] = findViewById(R.id.ddra_ib_star_5);

        findViewById(R.id.ddra_btn_rate).setOnClickListener(v -> {

            OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_ACTIVITE","RATE_CLICK",String.valueOf(score_dababy));

            if (score_dababy ==0) {
                Toast.makeText(activity_dababy, activity_dababy.getString(R.string.please_rate),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score_dababy >=4){
                if (App_dababyShow_dababyCustomize.getInstance_dababy().showSysReview_dababy()){
                    DababyApRvUtil.INSTANCE.launchRvFlow_dababy(activity_dababy, aBoolean -> {
                        if (!aBoolean) activity_dababy.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_dababy)));
                        dismiss();
                        return null;
                    });
                }else activity_dababy.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_dababy)));


            }else activity_dababy.startActivity(new Intent(activity_dababy, Feed_dababybackActivity.class));

            dismiss();
        });

        for (int i = 0; i< starIbs_dababy.length; i++){
            ImageButton scoreIb = starIbs_dababy[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore_dababy(fScore));
        }

    }

    private void setScore_dababy(int score_dababy){

        if (score_dababy >5) score_dababy =5;
        else if (score_dababy <1) score_dababy =1;

        this.score_dababy = score_dababy;

        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_ACTIVITE","STAR_CLICK",String.valueOf(score_dababy));

        for (int i = 0; i< starIbs_dababy.length; i++){
            ImageButton scoreIb = starIbs_dababy[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i< score_dababy ? R.drawable.icon_rate_star_light :R.drawable.icon_rate_star_gray);
        }
    }

    @Override
    public void show() {
        if (isShow_dababy) return;
        isShow_dababy = true;
        super.show();
        g5UB5();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_ACTIVITE","START","");
        DababyUtil.addActivedialogCount_dababy();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("DIALOG_REVIEW_ACTIVITE","END","");
        isShow_dababy = false;
    }


}
